%%%% SIMULATION INPUTS
    % N: number of nodes; 
    %height : Ymax of the area
    %width  : Xmax of the area
    %max_distance :comm distance which determine the neighbor degree
    %seed : Simulation seed, used to increase the repetitions
    

function [packet_delivery_ratios, routes_not_found] = main(N, locations, adjacency_matrix, badNodes, goodNodes, seed, step, trust)

    % initialize trust matrix
    trust_matrix = zeros(N,N);
    for i=1:N
        for j=1:N
            if i ~= j & adjacency_matrix(i, j) == 1
                trust_matrix(i, j) = 1;
            else
                trust_matrix(i, j) = -Inf;
            end
        end
    end

    % SIMULAION START
    packets_send = 0;
    packets_delivered = 0;
    packet_delivery_ratios = [];
    routes_not_found = 0;
    
    for sim_step=1:seed
        sim_step
        % 1 - choose random source and destination nodes

        source = ceil(N * rand());
        destination = ceil(N * rand());
        %fprintf('Source: %i Destination: %i\n', source, destination);

        % 2 - looking for a path

        paths = {};
        [paths, function_iterations] = dfs3(trust_matrix, [source], destination, paths, locations, 0, trust);


        % 3 - apply levels

        routing_path = [];

        if length(paths) > 0
            routing_path = paths{1};
            packets_send = packets_send + 1;
        else
            routes_not_found = routes_not_found + 1;
        end

        for i=1:length(routing_path)
            if (routing_path(i) == destination)
                fprintf('\nPacket delivered\n', currentSendingNode, curentReceivingNode);
                packets_delivered = packets_delivered + 1;
                break;
            end

            currentSendingNode = routing_path(i);
            curentReceivingNode = routing_path(i+1);

            randomNumber = rand(1);

            if trust_matrix(currentSendingNode, curentReceivingNode) > randomNumber & ~any(badNodes(:) == curentReceivingNode)
                %transmision is succesfull
                %fprintf('%i - %i : ', currentSendingNode, curentReceivingNode);
                trust_matrix(curentReceivingNode, currentSendingNode) = trust_matrix(curentReceivingNode, currentSendingNode) + 0.1;
            else
                %transmission is not succesfull
                %fprintf('\nFailed! Transmission from node %i to node %i was NOT succesfull!\n', currentSendingNode, curentReceivingNode);
                trust_matrix(curentReceivingNode, currentSendingNode) = trust_matrix(curentReceivingNode, currentSendingNode) - 0.1;

                % break will exit for loop
                break;
            end
        end
        
        if mod(sim_step, step) == 0
            packet_delivery_ratios(end+1) = packets_delivered/packets_send;
        end
    end

end
